#coding=utf-8
import os,time
from stscraper import get_deal, NoDealError,deal_head
from stutils import data2csv
from stutils import set_django_environ
set_django_environ()
from stdj.stock.models import Share

from stsettings import datadir
DEALDIR = "%s/deal"%datadir

def download_deal(share, start, end=None):
    '''下载某一只股票在一段时间内的所有成交明细数据, 并将数据放入csv文件''' 
    
    eds = share.exday_set.filter(date__gte=start)
    if end:
        eds = eds.filter(date__lt=end)
        
    if eds.count() == 0 :
        print "%s:没有交易 since %s."%(share, start)
        return
    
    d = "%s/%s"%(DEALDIR, share.code)
    if not os.path.exists(d):os.mkdir(d)

    for ed in eds:
        fn = "%s/%s.csv"%(d, ed.date)
        if os.path.exists(fn):continue
        
        try:
            data = get_deal(share.code, ed.date)
        except NoDealError,e:
            print "%s:No Deal on %s."%(share, ed.date)
            continue
        
        data2csv(data, deal_head, fn)
        
        print share.code, ed.date
    time.sleep(1)
    
import threading 

class DealThread(threading.Thread):
    def __init__(self, id, shares, start, end=None):
        threading.Thread.__init__(self)
        self.id = id
        self.shares = shares
        
        self.start_date = start
        self.end_date = end
        
    def run(self):
        print  "线程%i启动."%self.id
        for s in self.shares:
            download_deal(s, self.start_date, self.end_date)
            print "线程%i:完成下载%s的成交明细"%(self.id, s)
        print  "线程%i结束."%self.id    
if __name__ == '__main__':
    
    start = '2007-08-01'
    end = '2007-09-31'
    thread_num = 4
    
    shares = Share.objects.order_by('-code')
    step = shares.count()/thread_num + 1
    for i in range(thread_num):
        ss = shares[i*step: (i+1)*step]
        dt = DealThread(i, ss, start, end)
        dt = DealThread(i, ss, '2008-01-21')
        dt.start()
    
    print "主线程退出."    
    